import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LoginNew extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textID;
	private JTextField textPass;
	private JTextField textName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			LoginNew dialog = new LoginNew();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	
	public LoginNew() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		contentPanel.setLayout(new MigLayout("", "[11px][][46px,grow][][86px][86px]", "[20px][][]"));
		{
			JLabel lblName = new JLabel("Name");
			contentPanel.add(lblName, "cell 1 0");
		}
		{
			textName = new JTextField();
			contentPanel.add(textName, "cell 2 0");
			textName.setColumns(10);
		}
		{
			JLabel lblID = new JLabel("ID");
			lblID.setHorizontalAlignment(SwingConstants.LEFT);
			contentPanel.add(lblID, "cell 1 1,alignx left,aligny center");
		}
		{
			textID = new JTextField();
			textID.setColumns(10);
			contentPanel.add(textID, "cell 2 1 2 1,alignx left,aligny top");
		}
		{
			JLabel lblPass = new JLabel("Password");
			lblPass.setHorizontalAlignment(SwingConstants.CENTER);
			contentPanel.add(lblPass, "cell 1 2,alignx left,aligny bottom");
		}
		{
			textPass = new JTextField();
			textPass.setColumns(10);
			contentPanel.add(textPass, "cell 2 2 2 1,alignx left,aligny top");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//TODO: try to check DATABASE (!!!) if person exists
						String userName;
						userName = textName.getText();
						JOptionPane.showMessageDialog(null, "Welcome " + userName + "!!!   :)");
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	
	
	
	public LoginNew(final JFrame mainFrame) {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		contentPanel.setLayout(new MigLayout("", "[11px][][46px,grow][][86px][86px]", "[20px][][]"));
		{
			JLabel lblName = new JLabel("Name");
			contentPanel.add(lblName, "cell 1 0");
		}
		{
			textName = new JTextField();
			contentPanel.add(textName, "cell 2 0");
			textName.setColumns(10);
		}
		{
			JLabel lblID = new JLabel("ID");
			lblID.setHorizontalAlignment(SwingConstants.LEFT);
			contentPanel.add(lblID, "cell 1 1,alignx left,aligny center");
		}
		{
			textID = new JTextField();
			textID.setColumns(10);
			contentPanel.add(textID, "cell 2 1 2 1,alignx left,aligny top");
		}
		{
			JLabel lblPass = new JLabel("Password");
			lblPass.setHorizontalAlignment(SwingConstants.CENTER);
			contentPanel.add(lblPass, "cell 1 2,alignx left,aligny bottom");
		}
		{
			textPass = new JTextField();
			textPass.setColumns(10);
			contentPanel.add(textPass, "cell 2 2 2 1,alignx left,aligny top");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//TODO: try to check DATABASE (!!!) if person exists
						String userName;
						userName = textName.getText();
						JOptionPane.showMessageDialog(null, "Welcome " + userName + "!!!   :)");
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
						mainFrame.setVisible(true);
						
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
